*--------------------------------------------------------------; * Selects a probability proportional to size sample from a ; * finite population stored in a SAS data set called frame. ; * Elements are selected according to the variable SIZE. ; *--------------------------------------------------------------; %macro pps(noprint,frame=,setup=,npop=,n=,size=,sample=,pi=,rep=, seed=,mi=,m=); %if %length(&seed) = 0 %then %let seed = %str(0); %if %length(&sample)= 0 %then %let sample = %str(sample); %if %length(&frame) = 0 %then %let frame = %str(frame); %if %length(&pi) = 0 %then %let pi = %str(pi); %if %length(&n) = 0 %then %let n = %str(n); %if %length(&rep) = 0 %then %let rep = %str(rep); data &frame; set &frame; %if %length(&size) > 0 %then %do; cum_ + &size; %end; %else %do; cum_ + &mi; %end; data total_(keep = cum_); set &frame (keep = cum_) end = eof; if eof = 1 then do; output; call symput('tot_',trim(left(cum_))); end; run; data &frame; set &frame; u_ = cum_/&tot_; data unifs_( keep = u_ ); %if %length(&setup) > 0 %then %do; set &setup; %end; do i_ = 1 to &n; u_ = ranuni(&seed); output; end; proc sort data = unifs_; by u_; data &sample; set &frame unifs_; by u_; retain count_ 0; if cum_ = . then count_ = count_ + 1; else do; if count_ > 0 then do; do i_ = 1 to count_; %if %length(&size) > 0 %then %do; &pi = &size/&tot_; %end; %if %length(&mi) > 0 %then %do; mi = &mi; %end; &rep = i_; output; end; count_ = 0; end; end; drop count_ cum_ u_ i_; data &frame; set &frame; drop u_ cum_; %if %length(&noprint) = 0 %then %do; proc print data = &sample; title1 "Probability Proportional to Size Sample"; title2 "Output Data Set = &sample"; title3 "(Total Size = &tot_)"; %end; run; title; %mend pps;